/*
 * author      : dormi330
 * date        : 2018/6/26
 * project     : mybatis
 * description : adaptor 会员服务
 */

package com.deep.trade.adaptor.required.member;

import com.deep.trade.common.TradeException;
import com.deep.trade.adaptor.required.IdService;
import com.deep.trade.adaptor.required.MemberAddress;
import com.deep.trade.application.AppRuntimeEvn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class MemberServiceMock implements MemberService {

    private AppRuntimeEvn appRuntimeEvn;

    @Autowired
    public MemberServiceMock(AppRuntimeEvn appRuntimeEvn) {
        this.appRuntimeEvn = appRuntimeEvn;
    }

    private Member mockMember;
    private MemberAddress address;

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    private static Member mockMember() {
        Member member = new Member();
        member.setName("用户A");
        member.setId(IdService.MEMBER_ID);
        return member;
    }

    private static MemberAddress MockAddress4Member(String memberId) {
        MemberAddress address = new MemberAddress();
        address.setId(IdService.ADDRESS_ID);
        address.setMemberId(memberId);
        address.setProviceName("广东");
        address.setCityName("广州");
        address.setDistrictName("天河区");
        address.setStreetName("天园街道");
        address.setDetailAddress("棠下村999号");
        address.setPostCode("123456");
        address.setReceiverName("吴中勤");
        address.setReceiverPhone("18612341234");
        return address;
    }

    @PostConstruct
    public void init() {
        mockMember = mockMember();
        address = MockAddress4Member(mockMember.getId());
        logger.warn("MemberService 初始化, 添加测试数据 ");
    }

    public MemberAddress addressOfMember(String memberId, String addressId) throws Exception {
        // String tenantCode = appRuntimeEvn.getTenantCode();
        return address;
    }

    public Member memberOfToken() throws Exception {
        // String tenantCode = appRuntimeEvn.getTenantCode();
        String token = appRuntimeEvn.getToken();
        return IdService.MEMBER_TOKEN.equals(token) ? mockMember : null;
    }

    public Member assureMemberOfToken() throws Exception {
        Member member = memberOfToken();
        if (null == member) throw new TradeException("cann't get member info");
        return member;
    }
}
